package com.ruoyi.business.youdaoController.shangyou;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.utils.ChuanShanJiaSignUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.vo.R;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 穿山甲电商
 */
@RestController
@RequestMapping("ownCsjApi")
public class ChuanShanJiaCOntrollr {

    private final static String APP_ID = "5627500";
    private final static String VERSION = "1";
    private final static String SIGN_TYPE = "MD5";


    public static void main(String[] args) {
        String url = "https://www.ssptqt.com/ownCsjApi/list";

        int count = 0;
        int cursor = 1500;
        JSONArray resList = new JSONArray();
        while (count <= 20){
            JSONObject params = new JSONObject().fluentPut("cursor", cursor).fluentPut("count", 0)
                    .fluentPut("sort_by", 3).fluentPut("order_by", 1)
                    .fluentPut("city_code", "310000").fluentPut("category_id", 0).fluentPut("keyword", "");
            String body = HttpUtil.createPost(url).body(params.toJSONString(), "application/json").execute().body();
            JSONObject bodyJson = JSONObject.parseObject(body);
            JSONObject data = bodyJson.getJSONObject("data");
            cursor = data.getInteger("cursor");
            JSONArray productList = data.getJSONArray("product_list");
            resList.addAll(productList);
            count++;
            System.out.println("当前页["+count+"], 游标["+cursor+"]");
        }
        Map<Integer, Map<String, Object>> map = new HashMap<>();
        List<String> idList = new ArrayList<>();
//        System.out.println(resList.toJSONString());
        for (int i = 0; i < resList.size(); i++) {
            JSONObject item = resList.getJSONObject(i);
            Integer price = item.getInteger("price");
            String id = item.getString("id");
            Integer commissionAmount = item.getInteger("commission_amount");
            if(price <= 1000) {
                map.put(commissionAmount, item);
            }
        }
        //根据key 从大到小排序。 lamdba
        List<Map.Entry<Integer, Map<String, Object>>> list = new ArrayList<>(map.entrySet());
        list.sort((o1, o2) -> o2.getKey().compareTo(o1.getKey()));
        //转成json
        JSONArray res = new JSONArray();
        for (Map.Entry<Integer, Map<String, Object>> entry : list) {
            res.add(entry.getValue());
        }
        String str = res.toJSONString();
        FileUtil.writeString(str, "1.json", Charset.forName("UTF-8"));



    }

    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestBody JSONObject data) throws Exception {
        String url = "https://ecom.pangolin-sdk-toutiao.com/life/product/search";
        String reqId = IdUtils.fastSimpleUUID();
        long timestamp = System.currentTimeMillis() / 1000;
        ChuanShanJiaSignUtil.Req req1 = new ChuanShanJiaSignUtil.Req(APP_ID, SIGN_TYPE, timestamp, reqId, data.toJSONString());
        String sign = ChuanShanJiaSignUtil.getSign(req1);
        JSONObject req = new JSONObject();
        req.put("app_id", APP_ID);
        req.put("timestamp", timestamp);
        req.put("version", VERSION);
        req.put("sign", sign);
        req.put("sign_type", SIGN_TYPE);
        req.put("req_id", reqId);
        req.put("data", data.toJSONString());
        String body = HttpUtil.createPost(url).body(req.toJSONString(), "application/json").execute().body();
        return body;
    }

    @RequestMapping("/detail")
    @ResponseBody
    public Object detail(@RequestBody JSONObject data) throws Exception {
        String url = "https://ecom.pangolin-sdk-toutiao.com/life/product/detail";
        String reqId = IdUtils.fastSimpleUUID();
        long timestamp = System.currentTimeMillis() / 1000;
        ChuanShanJiaSignUtil.Req req1 = new ChuanShanJiaSignUtil.Req(APP_ID, SIGN_TYPE, timestamp, reqId, data.toJSONString());
        String sign = ChuanShanJiaSignUtil.getSign(req1);
        JSONObject req = new JSONObject();
        req.put("app_id", APP_ID);
        req.put("timestamp", timestamp);
        req.put("version", VERSION);
        req.put("sign", sign);
        req.put("sign_type", SIGN_TYPE);
        req.put("req_id", reqId);
        req.put("data", data.toJSONString());
        String body = HttpUtil.createPost(url).body(req.toJSONString(), "application/json").execute().body();
        return body;
    }

    @RequestMapping("/link")
    @ResponseBody
    public Object link(@RequestBody JSONObject data) throws Exception {
        String url = "https://ecom.pangolin-sdk-toutiao.com/life/product/link";
        String reqId = IdUtils.fastSimpleUUID();
        long timestamp = System.currentTimeMillis() / 1000;
        ChuanShanJiaSignUtil.Req req1 = new ChuanShanJiaSignUtil.Req(APP_ID, SIGN_TYPE, timestamp, reqId, data.toJSONString());
        String sign = ChuanShanJiaSignUtil.getSign(req1);
        JSONObject req = new JSONObject();
        req.put("app_id", APP_ID);
        req.put("timestamp", timestamp);
        req.put("version", VERSION);
        req.put("sign", sign);
        req.put("sign_type", SIGN_TYPE);
        req.put("req_id", reqId);
        req.put("data", data.toJSONString());
        String body = HttpUtil.createPost(url).body(req.toJSONString(), "application/json").execute().body();
        return body;
    }

    @RequestMapping("/command_parse")
    @ResponseBody
    public Object command_parse(@RequestBody JSONObject data) throws Exception {
        String url = "https://ecom.pangolin-sdk-toutiao.com/life/command_parse";
        String reqId = IdUtils.fastSimpleUUID();
        long timestamp = System.currentTimeMillis() / 1000;
        ChuanShanJiaSignUtil.Req req1 = new ChuanShanJiaSignUtil.Req(APP_ID, SIGN_TYPE, timestamp, reqId, data.toJSONString());
        String sign = ChuanShanJiaSignUtil.getSign(req1);
        JSONObject req = new JSONObject();
        req.put("app_id", APP_ID);
        req.put("timestamp", timestamp);
        req.put("version", VERSION);
        req.put("sign", sign);
        req.put("sign_type", SIGN_TYPE);
        req.put("req_id", reqId);
        req.put("data", data.toJSONString());
        String body = HttpUtil.createPost(url).body(req.toJSONString(), "application/json").execute().body();
        return body;
    }

    @RequestMapping("/orderList")
    @ResponseBody
    public Object orderList(@RequestBody JSONObject data) throws Exception {
        String url = "https://ecom.pangolin-sdk-toutiao.com/life/order/search";
        String reqId = IdUtils.fastSimpleUUID();
        long timestamp = System.currentTimeMillis() / 1000;
        ChuanShanJiaSignUtil.Req req1 = new ChuanShanJiaSignUtil.Req(APP_ID, SIGN_TYPE, timestamp, reqId, data.toJSONString());
        String sign = ChuanShanJiaSignUtil.getSign(req1);
        JSONObject req = new JSONObject();
        req.put("app_id", APP_ID);
        req.put("timestamp", timestamp);
        req.put("version", VERSION);
        req.put("sign", sign);
        req.put("sign_type", SIGN_TYPE);
        req.put("req_id", reqId);
        req.put("data", data.toJSONString());
        String body = HttpUtil.createPost(url).body(req.toJSONString(), "application/json").execute().body();
        return body;
    }

    @RequestMapping("/crowd_match")
    @ResponseBody
    public Object crowd_match(@RequestBody JSONObject data) throws Exception {
        String url = "https://ecom.pangolin-sdk-toutiao.com/life/crowd_match";
        String reqId = IdUtils.fastSimpleUUID();
        long timestamp = System.currentTimeMillis() / 1000;
        ChuanShanJiaSignUtil.Req req1 = new ChuanShanJiaSignUtil.Req(APP_ID, SIGN_TYPE, timestamp, reqId, data.toJSONString());
        String sign = ChuanShanJiaSignUtil.getSign(req1);
        JSONObject req = new JSONObject();
        req.put("app_id", APP_ID);
        req.put("timestamp", timestamp);
        req.put("version", VERSION);
        req.put("sign", sign);
        req.put("sign_type", SIGN_TYPE);
        req.put("req_id", reqId);
        req.put("data", data.toJSONString());
        String body = HttpUtil.createPost(url).body(req.toJSONString(), "application/json").execute().body();
        return body;
    }

    @RequestMapping("/activity_command_share")
    @ResponseBody
    public Object activity_command_share(@RequestBody JSONObject data) throws Exception {
        String url = "https://ecom.pangolin-sdk-toutiao.com/life/activity_command_share";
        String reqId = IdUtils.fastSimpleUUID();
        long timestamp = System.currentTimeMillis() / 1000;
        ChuanShanJiaSignUtil.Req req1 = new ChuanShanJiaSignUtil.Req(APP_ID, SIGN_TYPE, timestamp, reqId, data.toJSONString());
        String sign = ChuanShanJiaSignUtil.getSign(req1);
        JSONObject req = new JSONObject();
        req.put("app_id", APP_ID);
        req.put("timestamp", timestamp);
        req.put("version", VERSION);
        req.put("sign", sign);
        req.put("sign_type", SIGN_TYPE);
        req.put("req_id", reqId);
        req.put("data", data.toJSONString());
        String body = HttpUtil.createPost(url).body(req.toJSONString(), "application/json").execute().body();
        return body;
    }
}
